package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.awt.desktop.PreferencesEvent;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping ("/admin/dish")
public class DishController {


    @Autowired
    DishServiceImpl dishService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result insertDish(@RequestBody DishDTO dto){
        log.info("新增菜品信息："+ dto);
        dishService.insertDish(dto);

        //清除缓存信息
        String key="dish_"+dto.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }


    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result pageQuery(DishPageQueryDTO pageQueryDTO){
        log.info("分页参数："+ pageQueryDTO);
        PageResult pageResult = dishService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 状态修改
     *
     * @param status
     * @param id
     * @return
     */


    @ApiOperation("状态修改")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        log.info("状态参数："+ status);
        log.info("菜品id：" +id);
        dishService.updateStatus( status, id);

        //将所有的分类的套餐关联的菜品缓存都删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**
     * 根据id查菜品  数据回显
     * @param id
     * @return
     */

    @ApiOperation("根据id查菜品  数据回显")
    @GetMapping("/{id}")
    public Result getDishById(@PathVariable Long id){
        log.info("查询id："+id);
        //Result dishById = dishService.getDishById(id);
        return  dishService.getDishById(id);
    }


    /**
     * 根据分类id查菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查菜品")
    @GetMapping("/list")
    public Result getDishListByCategoryId(Long categoryId){
        log.info("分类id: "+categoryId);
        List<Dish> dishListByCategoryId = dishService.getDishListByCategoryId(categoryId);
        return Result.success(dishListByCategoryId);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishVO dishVO){
        log.info("修改参数："+ dishVO);
        dishService.updateDish(dishVO);

        //将所有的分类的套餐关联的菜品缓存都删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }



    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteById(@RequestParam List<Long> ids){
        log.info("批量删除参数："+ids);

        //将所有的分类的套餐关联的菜品缓存都删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return dishService.deleteById(ids);
    }

}
